# -*- coding: utf-8 -*-
'''
利用sklearn，对数据进行归一化处理
    对测试数据的归一化处理：
        使用的是训练数据的均值和方差
            因此，要保存下来训练数据集得到的均值和方差
'''
import numpy as np
from sklearn import datasets

iris = datasets.load_iris()

X = iris["data"]
y = iris["target"]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split( X, y, test_size = 0.2, random_state = 666 )

# sklearn中的StandardScaler
from sklearn.preprocessing import StandardScaler
standardScaler = StandardScaler()
standardScaler.fit( X_train )
standardScaler.mean_ #array([ 5.83416667,  3.0825    ,  3.70916667,  1.16916667])
standardScaler.scale_ #array([ 0.81019502,  0.44076874,  1.76295187,  0.75429833])
X_train = standardScaler.transform( X_train )
X_test = standardScaler.transform( X_test )

from sklearn.neighbors import KNeighborsClassifier

my_knn = KNeighborsClassifier( n_neighbors = 3 )
my_knn.fit( X_train, y_train )
accuracy = my_knn.score( X_test, y_test ) # 1.0